package com.interview.service.impl;


import com.interview.entity.GroupInfo;
import com.interview.entity.Schedule;
import com.interview.mapper.GroupInfoMapper;
import com.interview.service.DataDownLoadService;
import com.interview.service.GroupInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@Service
public class GroupInfoServiceImpl implements GroupInfoService {
    @Autowired
    private GroupInfoMapper groupInfoMapper;
    @Autowired
    private DataDownLoadService dataDownLoadService;

    @Override
    public List<Schedule> generateDrawLotsProcess(GroupInfo groupInfo) {
        String groupId = groupInfo.getGroupId();
        int account = Integer.parseInt(groupInfo.getAccount());
        int[] numbers = IntStream.rangeClosed(1, account).toArray();
        List<Schedule> drawLotsSchedules = new ArrayList<>();
        for (int number : numbers) {
            Schedule drawLotsSchedule = new Schedule(groupId, String.valueOf(number));
            drawLotsSchedules.add(drawLotsSchedule);
        }
        return drawLotsSchedules;
    }

    @Override
    public List<GroupInfo> getAllGroupInfo() {
        return groupInfoMapper.selectList(null);
    }

    @Override
    public List<GroupInfo> getGroupInfoByIdsFromDataCenter(String groupIds) throws IOException {
        List<GroupInfo> groupInfos=dataDownLoadService.getGroupInfoByIdsFromDataCenter(groupIds);
        return groupInfos;
    }

    @Override
    public int addGroupInfos(List<GroupInfo> groupInfos) {
        int i=0;
        for (GroupInfo groupInfo : groupInfos) {
            groupInfoMapper.insert(groupInfo);
            i++;
        }
        return i;
    }


}
